<!DOCTYPE html>
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>flv.js demo</title>
    <style>
        .mainContainer {
            display: block;
            width: 1024px;
            margin-left: auto;
            margin-right: auto;
        }

        .urlInput {
            display: block;
            width: 100%;
            margin-left: auto;
            margin-right: auto;
            margin-top: 8px;
            margin-bottom: 8px;
        }

        .centeredVideo {
            display: block;
            width: 100%;
            height: 576px;
            margin-left: auto;
            margin-right: auto;
            margin-bottom: auto;
        }

        .controls {
            display: block;
            width: 100%;
            text-align: left;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>
<body>
<div class="mainContainer">
    <video id="videoElement" class="centeredVideo" controls autoplay width="1024" height="576" muted>Your browser is too old which doesn't support HTML5 video.</video>
</div>
<br>
<div class="controls">
    <!--<button onclick="flv_load()">加载</button>-->
    <button onclick="flv_play()">播放</button>
    <button onclick="flv_start()">开始</button>
    <button onclick="flv_pause()">暂停</button>
    <button onclick="flv_destroy()">停止</button>
    <input style="width:100px" type="text" name="seekpoint" />
    <button onclick="flv_seekto()">跳转</button>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/flv.js/1.5.0/flv.min.js"></script>

<script>
    var player = document.getElementById('videoElement');
    if (flvjs.isSupported()) {
        var flvPlayer = flvjs.createPlayer({
            type: 'flv',
            url: 'http://10.0.0.110:80/live?port=1935&app=mlive&stream=3TPCP37505RGFDH',
            "isLive": true,
            hasAudio: false,
            hasVideo: true,
            //withCredentials: false,
            //cors: true
        }, {
            enableWorker: true,	// 开启多线程
            enableStashBuffer: false,
            lazyLoad: false,
            lazyLoadMaxDuration: 0,
            lazyLoadRecoverDuration: 0,
            deferLoadAfterSourceOpen: false,
            fixAudioTimestampGap: true,
            autoCleanupSourceBuffer: true,
        });
        flvPlayer.attachMediaElement(videoElement);
        player.load();
        player.play();
    }

    function flv_start() {
        player.play();
    }

    function flv_pause() {
        player.pause();
    }

    function flv_destroy() {
        player.pause();
        player.unload();
        player.detachMediaElement();
        player.destroy();
        player = null;
    }

    function flv_seekto() {
        player.currentTime = parseFloat(document.getElementsByName('seekpoint')[0].value);
    }

    function flv_play(){
        // 创建一个新的 XMLHttpRequest 对象
        var xhr = new XMLHttpRequest();

        var queryParams = getQueryParams(window.location.href);
        console.log(queryParams)
        // 获取地址协议（如http或https）
        var protocol = window.location.protocol;
        console.log("协议： " + protocol);

        // 获取IP地址
        var ip = window.location.hostname;
        console.log("IP地址： " + ip);
        // 获取端口号
        var port = window.location.port;
        console.log("端口号： " + port);

        // 配置请求
        var requestUrl = protocol+"//"+ip+":"+port+"/video/play?"+queryParams
        console.log(requestUrl)
        var code = queryParams.substr(queryParams.indexOf("=")+1, queryParams.indexOf("&") - 5)
        var rtsp = queryParams.substr(queryParams.indexOf("&") +1);
        var data = {
            code: code,
            rtsp: rtsp
        };
        console.log(data)

        xhr.open('POST', requestUrl, true);
        // 设置请求头，指定内容类型为JSON
        xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

        // 设置请求完成后的回调函数
        xhr.onload = function() {
            if (xhr.status >= 200 && xhr.status < 400) {
                // 请求成功，处理返回的数据
                console.log(xhr.responseText);
                flvPlayer.url = xhr.responseText;
                flvPlayer.play();//播放
            } else {
                // 请求失败，处理错误
                console.error('请求失败，状态码：' + xhr.status);
            }
        };

        // 设置请求过程中发生错误的回调函数
        xhr.onerror = function() {
            console.error('请求过程中发生错误');
        };

        // 发送请求
        xhr.send(JSON.stringify(data));
    }

    // 获取请求参数
    function getQueryParams(url) {
        var params = url.substr(url.indexOf('?') + 1);
        return params;
    }


</script>
</body>
</html>
